package com.glory.wxelectricitypda.adapter;

import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.bean.FTDataAcqBinBean;
import com.glory.wxelectricitypda.bean.FTDataAcqQueryBean;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FTDataAcqBinAdapter extends BaseQuickAdapter<FTDataAcqBinBean.EDCSETLISTDTO.EDCSETBINLINELISTDTO, BaseViewHolder> {
    private String mainqty;

    private List<Boolean> isFirstTimeList;

    private boolean type = false;


    public void setType(boolean type) {
        this.type = type;
    }

    public void setMainqty(String mainqty) {
        this.mainqty = mainqty;
    }

    public FTDataAcqBinAdapter(@Nullable List<FTDataAcqBinBean.EDCSETLISTDTO.EDCSETBINLINELISTDTO> data, String mainqty) {
        super(R.layout.adapter_ft_data_acq_bin, data);
        this.mainqty = mainqty;
    }

    @Override
    public void setNewData(@Nullable List<FTDataAcqBinBean.EDCSETLISTDTO.EDCSETBINLINELISTDTO> data) {
        super.setNewData(data);
        if (data!=null){
            this.isFirstTimeList = new ArrayList<>(Collections.nCopies(data.size(), true));
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, FTDataAcqBinBean.EDCSETLISTDTO.EDCSETBINLINELISTDTO item) {

        helper.setText(R.id.binName,item.getNAME());
        helper.setText(R.id.binReak,item.getDESCRIPTION());
        helper.setText(R.id.binYie,item.getBinYie());
        helper.setText(R.id.binQty,item.getVALUE());
        EditText editText = helper.getView(R.id.binQty);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (type){
                    if (isFirstTimeList.get(helper.getAdapterPosition())){
                        isFirstTimeList.set(helper.getAdapterPosition(), false);
                    }else {
                        item.setVALUE(editable.toString());
                        if (!item.getVALUE().isEmpty()){
                            DecimalFormat decimalFormat = new DecimalFormat("0.00%");
                            String formatted = decimalFormat.format((Double.valueOf(item.getVALUE()) /Double.valueOf(mainqty)));
                            helper.setText(R.id.binYie,String.valueOf(formatted));
                            item.setBinYie(String.valueOf(formatted));
                        }else {
                            helper.setText(R.id.binYie,"0%");
                            item.setBinYie("0%");
                        }
                    }
                }else {
                    item.setVALUE(editable.toString());
                    if (!item.getVALUE().isEmpty()){
                        DecimalFormat decimalFormat = new DecimalFormat("0.00%");
                        String formatted = decimalFormat.format((Double.valueOf(item.getVALUE()) /Double.valueOf(mainqty)));
                        helper.setText(R.id.binYie,String.valueOf(formatted));
                        item.setBinYie(String.valueOf(formatted));
                    }else {
                        helper.setText(R.id.binYie,"0%");
                        item.setBinYie("0%");
                    }
                }
            }
        });
    }
}
